from rest_framework.response import Response
# Create your views here.
from dwebsocket.decorators import accept_websocket #引入dwbsocket的accept_websocket装饰器
import json


@accept_websocket
def connect(request):
    # 判断是不是ws请求
    if request.is_websocket():
        while True:
            message = request.websocket.wait()
            print(message)
            if not message:
                print('客户端关闭')
                break
            else:
                msg=str(message, encoding = "utf-8")
                if msg == "connect":
                    print("客户端链接成功")
            # 发送信息
            request.websocket.send(json.dumps({"code":200,"msg":"连接成功"}).encode("'utf-8'"))
            print('发送成功')
       